package com.rocksea.app.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.AutoCompleteTextView;

import com.rocksea.app.MyApplication;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Mrw on 2017/7/13.
 *
 */

public class PerUtils {


    public static boolean getBoolean(String key, boolean defValue) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }

    public static void setBoolean(String key, boolean value) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        sp.edit().putBoolean(key, value).commit();
    }

    public static void setString( String key, String value) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        sp.edit().putString(key, value).commit();
    }


    public static String getString( String key, String defValue) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }
    public static String getString(Context mContext, String key, String defValue) {
        SharedPreferences sp = mContext.getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }
    public static void setInt( String key, int value) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt( String key, int defValue) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }
    public static void setlong( String key, long value) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        sp.edit().putLong(key, value).commit();
    }

    public static long getlong(String key, long defValue) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("config",
                Context.MODE_PRIVATE);
        return sp.getLong(key,defValue);
    }


    public static Date stringToDate(String time){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }



    public static String dateToStr(Date date)
    {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String dateToDay(Date date)
    {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String t= format.format(date);
        String[] times = t.split(" ");
        return times[0];
    }


    public static double getTwoData(double data)
    {
        BigDecimal b = new BigDecimal(data);
        return b.setScale(2, RoundingMode.HALF_UP).doubleValue();
    }

    public static double getThrerData(double data)
    {
        BigDecimal b = new BigDecimal(data);
        return b.setScale(3, RoundingMode.HALF_UP).doubleValue();
    }


}
